
#import "TorrentFileListViewCell.h"

@implementation TorrentFileListViewCell

//
// file name label
//
@synthesize fileNameLabel													= _fileNameLabel;

//
// file size label
//
@synthesize fileSizeLabel													= _fileSizeLabel;

//
// init
//
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
	//
	// call super
	//
	self																	= [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if(!self)
		return nil;

	//
	// create file name label
	//
	self.fileNameLabel														= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	self.fileNameLabel.textAlignment										= UITextAlignmentLeft;
	self.fileNameLabel.lineBreakMode										= UILineBreakModeWordWrap;
	self.fileNameLabel.numberOfLines										= 0;
	self.fileNameLabel.font													= [UIFont boldSystemFontOfSize:17];
	self.fileNameLabel.backgroundColor										= [UIColor clearColor];
	[self.contentView addSubview:self.fileNameLabel];
	
	//
	// create file size label
	//
	self.fileSizeLabel														= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	self.fileSizeLabel.textColor											= [UIColor grayColor];
	self.fileSizeLabel.textAlignment										= UITextAlignmentLeft;
	self.fileSizeLabel.lineBreakMode										= UILineBreakModeTailTruncation;
	self.fileSizeLabel.font													= [UIFont systemFontOfSize:14];
	self.fileSizeLabel.backgroundColor										= [UIColor clearColor];
	[self.contentView addSubview:self.fileSizeLabel];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.fileNameLabel														= nil;
	self.fileSizeLabel														= nil;
	
	[super dealloc];
}

//
// layout subviews
//
- (void)layoutSubviews
{
	[super layoutSubviews];
	
	CGRect bounds															= self.contentView.bounds;
	CGSize textSize															= {bounds.size.width - 20, 100000};
	CGSize fileNameSize														= [self.fileNameLabel.text sizeWithFont:self.fileNameLabel.font constrainedToSize:textSize lineBreakMode:self.fileNameLabel.lineBreakMode];
	CGRect frame															= CGRectMake(10, 2, fileNameSize.width, fileNameSize.height);
	self.fileNameLabel.frame												= frame;
	CGSize fileSizeSize														= [self.fileSizeLabel.text sizeWithFont:self.fileSizeLabel.font constrainedToSize:textSize lineBreakMode:self.fileSizeLabel.lineBreakMode];
	self.fileSizeLabel.frame												= CGRectMake(frame.origin.x, frame.origin.y + frame.size.height, fileSizeSize.width, fileSizeSize.height);
}

//
// row heigth
//
+ (CGFloat)rowHeightForFileName:(NSString*)fileName fileSize:(NSString*)fileSize maxWidth:(CGFloat)maxWidth
{
	CGSize textSize															= {maxWidth - 20, 100000};
	CGSize fileNameSize														= [fileName sizeWithFont:[UIFont boldSystemFontOfSize:17] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
	CGSize fileSizeSize														= [fileSize sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:textSize lineBreakMode:UILineBreakModeTailTruncation];
	return fileNameSize.height + fileSizeSize.height + 4;
}

@end
